﻿using Ldt.DataEngine.DataFlowParse.Model;
using LdtDataEngin.ModuleDetail.AnalysisModule.CaseCauseBaseInfoExtr;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.Common.CommHelp;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.Collection;
using LdtJudge.DataEngine.Model.DataModel.SysCommon;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace LdtDataEngin.ModuleDetail.CommonModule.DataSourceSplit
{
    /// <summary>
    /// 数据拆分模块，将案件要素中心进行拆分
    /// </summary>
    public class DataSourceSplit
    {

        private DataContext dataContext { get; }

        private string modleNmae { get; set; }

        /// <summary>
        /// 文书在案件要素中心中的定位配置
        /// </summary>
        private List<DOCLocationModel> locationModelList { get; set; }


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dataContextIn">上下文</param>
        /// <param name="moudleName">模块名称</param>
        public DataSourceSplit(DataContext dataContextIn, string moudleName)
        {
            modleNmae = moudleName;
            dataContext = dataContextIn;
            
        }

        /// <summary>
        /// 基础数据拆分保存模块
        /// </summary>
        /// <param name="BaseElement">基础xml要素</param>
        /// <param name="MergeElement"></param>
        public void StartLoade(string BaseElement, string MergeElement)
        {
           
            DOCLocationModel locationModel;

            XmlDocument xmlMergeElment = new XmlDocument();
            xmlMergeElment.LoadXml(MergeElement);

            XmlDocument xmlBaseElement = new XmlDocument();
            xmlBaseElement.LoadXml(BaseElement);

            if (!string.IsNullOrEmpty( dataContext.DataLayerAns.CauseId))
            {
               
                CaseCauseExtract caseCauseExtract = new CaseCauseExtract(dataContext, modleNmae) { origitxt = dataContext.DataLayerAns.FengXiCanShu.Juanzongneirong };
                caseCauseExtract.CaseCauseExt();
            }

            var configModel = dataContext.DataLayerSys.GetConfigModel(modleNmae);
            if (configModel.ConfigDetail != null)
            {
                foreach (ConfigModel item in configModel.ConfigDetail)
                {
                    locationModelList = GetLocationModels(item.ConfigContent,xmlMergeElment);
                    dataContext.DataLayerSys.locationModelList = locationModelList;
                }
            }
            if (locationModelList!=null && locationModelList.Count>0)
            {
                locationModel= locationModelList.FirstOrDefault(x => x.DocType.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower());
                MergElementSub(locationModel, xmlMergeElment);
                BseElementSub(locationModel, xmlBaseElement);
            }
        }

        /// <summary>
        /// 解析分析后要素
        /// </summary>
        /// <param name="locationModel"></param>
        /// <param name="xmlMergeElment"></param>
        private void MergElementSub(DOCLocationModel locationModel,XmlDocument xmlMergeElment) 
        {
            string outxml = xmlMergeElment.OuterXml;
            XmlUtilCommon xmlUtilCommon = new XmlUtilCommon() { dataContext = dataContext };
            
            
            if (!string.IsNullOrEmpty(locationModel.EelementPaht))
            {
                XmlNode xmlNodeSubCause = xmlMergeElment.SelectSingleNode(locationModel.EelementPaht);
                if (xmlNodeSubCause != null)
                {
                    XmlDocument xmlDocSubCause = new XmlDocument();
                    xmlDocSubCause.LoadXml(xmlNodeSubCause.InnerXml);
                    DataSoursModel dataSubCauseModel = xmlUtilCommon.GetXMLByDataModelBackFill(xmlDocSubCause, "CurrentData");
                    DataModel dataModelRoot = dataSubCauseModel.dataModelList.FirstOrDefault(a => a.ParentId == "");
                    string causeid = dataModelRoot.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "案由id").sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue;
                    List<DataModel> dataModelList = dataSubCauseModel.dataModelList.Where(a => a.ParentId == dataModelRoot.ID).ToList();
                    foreach (DataModel model in dataModelList)
                    {
                        model.ParentId = dataContext.DataLayerAns.CauseId;
                    }
                    dataModelRoot.ID = dataContext.DataLayerAns.CauseId;
                    dataContext.DataLayerAns.ElementAnalsisDataModelResults.Add(causeid,dataSubCauseModel.dataModelList);
                    outxml = outxml.Replace(xmlNodeSubCause.InnerXml, "");
                }
            }
            if (!string.IsNullOrEmpty(locationModel.MergeElmentPath))
            {
                XmlNode xmlNodeCause = xmlMergeElment.SelectSingleNode(locationModel.MergeElmentPath);
                if (xmlNodeCause != null)
                {
                    XmlDocument xmlDocCause = new XmlDocument();
                    xmlDocCause.LoadXml(xmlNodeCause.InnerXml);
                    DataSoursModel dataSoursCause = xmlUtilCommon.GetXMLByDataModelBackFill(xmlDocCause, "CurrentData");
                    dataContext.DataLayerAns.MergeElmentDataModelList = dataSoursCause.dataModelList;
                    outxml = outxml.Replace(xmlNodeCause.InnerXml, "");
                }
            }
            
            XmlDocument xmlDocCase = new XmlDocument();
            xmlDocCase.LoadXml(outxml);
            DataSoursModel dataSoursCase = xmlUtilCommon.GetXMLByDataModelBackFill(xmlDocCase, "CurrentData");
            dataContext.DataLayerAns.AnalysisResultsDataModel = dataSoursCase;
            dataContext.DataLayerAns.AnalysisResultsXml = xmlUtilCommon.DataModelByXml(dataSoursCase.dataModelList, "");
        }

        /// <summary>
        /// 解析优选后结果要素
        /// </summary>
        /// <param name="locationModel"></param>
        /// <param name="xmlMergeElment"></param>
        private void BseElementSub(DOCLocationModel locationModel, XmlDocument xmlBaseElment) 
        {

            string outxml = xmlBaseElment.OuterXml;
            XmlUtilCommon xmlUtilCommon = new XmlUtilCommon() { dataContext = dataContext };


            if (!string.IsNullOrEmpty(locationModel.EelementPaht))
            {
                XmlNode xmlNodeSubCause = xmlBaseElment.SelectSingleNode(locationModel.EelementPaht);
                if (xmlNodeSubCause != null)
                {
                    XmlDocument xmlDocSubCause = new XmlDocument();
                    xmlDocSubCause.LoadXml(xmlNodeSubCause.InnerXml);
                    DataSoursModel dataSubCauseModel = xmlUtilCommon.GetXMLByDataModelBackFill(xmlDocSubCause, "LastResultDate");
                    DataModel dataModelRoot = dataSubCauseModel.dataModelList.FirstOrDefault(a => a.ParentId == "");
                    List<DataModel> dataModelList = dataSubCauseModel.dataModelList.Where(a => a.ParentId == dataModelRoot.ID).ToList();
                    foreach (DataModel model in dataModelList)
                    {
                        model.ParentId = dataContext.DataLayerAns.CauseId;
                    }
                    dataModelRoot.ID = dataContext.DataLayerAns.CauseId;
                    dataContext.DataLayerOpt.ElementAnalsisDataModelResults = dataSubCauseModel.dataModelList;
                    outxml = outxml.Replace(xmlNodeSubCause.InnerXml, "");
                }
            }
            if (!string.IsNullOrEmpty(locationModel.MergeElmentPath))
            {
                XmlNode xmlNodeCause = xmlBaseElment.SelectSingleNode(locationModel.MergeElmentPath);
                if (xmlNodeCause != null)
                {
                    XmlDocument xmlDocCause = new XmlDocument();
                    xmlDocCause.LoadXml(xmlNodeCause.InnerXml);
                    DataSoursModel dataSoursCause = xmlUtilCommon.GetXMLByDataModelBackFill(xmlDocCause, "LastResultDate");
                    dataContext.DataLayerOpt.MergeElmentDataModelList = dataSoursCause.dataModelList;
                    outxml = outxml.Replace(xmlNodeCause.InnerXml, "");
                }
            }

           
            XmlDocument xmlDocCase = new XmlDocument();
            xmlDocCase.LoadXml(outxml);
            DataSoursModel dataSoursCase = xmlUtilCommon.GetXMLByDataModelBackFill(xmlDocCase, "LastResultDate");
            dataContext.DataLayerOpt.AnalysisResultsDataModel = dataSoursCase.dataModelList;

        }

        /// <summary>
        /// xml转数据对象集合
        /// </summary>
        /// <param name="xmlStr"></param>
        /// <returns></returns>
        private List<DOCLocationModel> GetLocationModels(string xmlStr,XmlDocument xmlDocument)
        {
            ParseLocationConfig locationConfigM = new ParseLocationConfig();
            List<CTLocationModel> cTLocations = locationConfigM.GetLocationModels(xmlStr,xmlDocument, dataContext.DataLayerAns.CaseType);
            return locationConfigM.GetDOCLocations(dataContext.DataLayerAns.CaseType, cTLocations);
        }
    }
}
